package com.dy.web.controller.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.dy.common.core.domain.entity.SysDept;
import com.dy.common.core.domain.entity.SysUser;
import com.dy.common.utils.DateUtils;
import com.dy.common.utils.StringUtils;
import com.dy.system.domain.MedicalInstitution;
import com.dy.system.service.*;
import com.github.pagehelper.PageHelper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.dy.common.annotation.Log;
import com.dy.common.core.controller.BaseController;
import com.dy.common.core.domain.AjaxResult;
import com.dy.common.enums.BusinessType;
import com.dy.system.domain.InspectionReport;
import com.dy.common.utils.poi.ExcelUtil;
import com.dy.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author anps
 * @date 2022-08-04
 */
@RestController
@RequestMapping("/system/report")
public class InspectionReportController extends BaseController{
    @Autowired
    private IInspectionReportService inspectionReportService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private IMedicalInstitutionService medicalInstitutionService;
    @Autowired
    private ISysDictDataService sysDictDataService;


    @GetMapping("/dp/list")
    public AjaxResult getDpReportList(@RequestParam Long institutionId,
                                      @RequestParam(required = false) String year){
        InspectionReport inspectionReport = new InspectionReport();
        inspectionReport.setInstitutionId(institutionId);
        inspectionReport.setYear(year);

        List<InspectionReport> reportList = new ArrayList<>();
        //查询检测报告
        String img = "https://dyep.cc/prod-api";
        for (int a = 1; a <=4; a ++){
            inspectionReport.setSession(String.valueOf(a));
            InspectionReport report = inspectionReportService.selectInspectionReportDp(inspectionReport);
            if (report == null){
                report = new InspectionReport();
                report.setSession(String.valueOf(a));
                report.setStatus("0");
                report.setDateSection(getSubDateStr(String.valueOf(a)));
            }else{
                report.setDateSection(getSubDateStr(String.valueOf(a)));
                report.setStatus("1");
            }
            report.setReportUrl(StringUtils.isNoneBlank(report.getReportUrl()) ? img + report.getReportUrl() : "");
            String jdName = sysDictDataService.selectDictLabel("report_sc_jd", report.getSession());
            report.setJdName(jdName);
            String dataStr = year;
            if (StringUtils.isBlank(year)){
                dataStr = DateUtils.parseDateToStr(DateUtils.YYYY,new Date());
            }
            report.setYeayName(dataStr);
            report.setYear(dataStr);
            reportList.add(report);
        }
        return AjaxResult.success(reportList);
    }


    public String getSubDateStr(String session){
        String dataFw = null;
        switch (session){
            case "1":
                dataFw = "01月01日~03月31日";
                break;
            case "2":
                dataFw = "04月01日~06月30日";
                break;
            case "3":
                dataFw = "06月01日~09月30日";
                break;
            default:
                dataFw = "10月01日~12月31日";
                break;
        }
        return dataFw;
    }


    /***
     * 检测报告applet接口
     * @param institutionId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/applet/list")
    public AjaxResult getReportList(@RequestParam Long institutionId,
                                            @RequestParam Integer pageNo, @RequestParam Integer pageSize){
        InspectionReport report = new InspectionReport();
        report.setInstitutionId(institutionId);
        PageHelper.startPage(pageNo,pageSize,"id desc");
        List<InspectionReport> reportList = inspectionReportService.selectInspectionReportList(report);
        for (InspectionReport r : reportList) {
            String dataStr = DateUtils.parseDateToStr(DateUtils.YYYY,r.getCreateTime());
            String session = r.getSession();
            String dataFw = null;
            switch (session){
                case "1":
                    dataFw = "第一季度·01月01日~03月31日";
                    break;
                case "2":
                    dataFw = "第二季度·04月01日~06月30日";
                    break;
                case "3":
                    dataFw = "第三季度·06月01日~09月30日";
                    break;
                default:
                    dataFw = "第四季度·10月01日~12月31日";
                    break;
            }
            r.setSplicingStr(new StringBuffer(dataStr).append("年度·").append(dataFw).toString());
            r.setYear(dataStr);
            String jdName = sysDictDataService.selectDictLabel("report_sc_jd", report.getSession());
            r.setJdName(jdName);
        }
        return AjaxResult.success(reportList);
    }


    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:report:list')")
    @GetMapping("/list")
    public TableDataInfo list(InspectionReport inspectionReport){
        startPage();
        List<InspectionReport> list = inspectionReportService.selectInspectionReportList(inspectionReport);
        list.forEach(r ->{
            SysUser sysUser = sysUserService.selectUserById(r.getUserId());
            if (sysUser != null){
                r.setCreateUserName(sysUser.getUserName());
            }
            SysDept sysDept = sysDeptService.selectDeptById(r.getDeptId());
            if (sysDept != null){
                r.setDeptName(sysDept.getDeptName());
            }
            MedicalInstitution institution = medicalInstitutionService.selectMedicalInstitutionById(r.getInstitutionId());
            if (institution != null){
                r.setInstitutionName(institution.getName());
            }
            String category = sysDictDataService.selectDictLabel("report_sc_jd", r.getSession());
            r.setJdName(category);
        });
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:report:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InspectionReport inspectionReport){
        List<InspectionReport> list = inspectionReportService.selectInspectionReportList(inspectionReport);
        ExcelUtil<InspectionReport> util = new ExcelUtil<InspectionReport>(InspectionReport.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:report:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return AjaxResult.success(inspectionReportService.selectInspectionReportById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:report:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InspectionReport inspectionReport){
        inspectionReport.setUserId(getUserId());
        return toAjax(inspectionReportService.insertInspectionReport(inspectionReport));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:report:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InspectionReport inspectionReport){
        return toAjax(inspectionReportService.updateInspectionReport(inspectionReport));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:report:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(inspectionReportService.deleteInspectionReportByIds(ids));
    }
}
